const express = require('express')
const router = express.Router()
const http = require('http')
const cheerio = require('cheerio')
const request = require('superagent')

function fileterText(text) {
  return text.replace(/\n/g, '').replace(/\s/g, '')
}

router.get('/', (req, res, next) => {
  res.render('index', { title: '简单nodejs爬虫' })
})

router.get('/getJobs', (req, res, next) => {
  let page = req.query.page
  console.log('page->' + page)
  const reptileUrl = 'http://www.jianshu.com/'

  request
    .get(reptileUrl)
    .set(
      'User-Agent',
      'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3464.0 Safari/537.36'
    )
    .end((err, response) => {
      if (err) {
        throw Error(err)
      }
      let $ = cheerio.load(response.text)
      let data = []
      $('#list-container .note-list li').each((i, elem) => {
        let _this = $(elem)
        data.push({
          id: _this.attr('data-note-id'),
          thumbnails: _this.find('.wrap-img img').attr('src'),
          slug: _this
            .find('.title')
            .attr('href')
            .replace(/\/p\//, ''),
          title: fileterText(_this.find('.title').text()),
          abstract: fileterText(_this.find('.abstract').text()),
          meta: {
            nickname: fileterText(_this.find('.nickname').text()),
            link: _this
              .find('.nickname')
              .attr('href')
              .replace(/\/u\//, ''),
            comments_count: +fileterText(
              _this
                .find('.ic-list-comments')
                .parent()
                .text()
            ),
            likes_count: +fileterText(
              _this
                .find('.ic-list-like')
                .parent()
                .text()
            )
          }
        })
      })
      res.send({
        status: 0,
        data: data
      })
    })
})

module.exports = router
